package com.cgycms.webdeploy.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.cgycms.webdeploy.aop.Log;
import com.cgycms.webdeploy.auth.AuthLoad;
import com.cgycms.webdeploy.auth.UserModel;
import com.cgycms.webdeploy.common.DeployConstant;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

/**
 * @ClassName : LoginController
 * @Description : 登录
 * @Author : 王力超
 * @Date: 2020-11-05 10:37
 */
@Controller
public class LoginController extends BaseController {


    @GetMapping("/login")
    public String login() {
        UserModel user = user();
        if (user != null) {
            return "redirect:/";
        }
        return "loginPage";
    }


    @GetMapping("/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) {
        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 20);
        try {
            request.getSession().setAttribute(DeployConstant.SESSION_CAPTCHA_ID, captcha.getCode());
            captcha.write(response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @PostMapping("/login")
    @Log(value = "执行登录操作")
    @ResponseBody
    public Integer login(String username, String password, @RequestParam("code") String code) {
        if (StrUtil.isBlank(username) || StrUtil.isBlank(password)) {
            return DeployConstant.USER_LOGIN_FAIL;
        }
        String localCode = (String) getSession().getAttribute(DeployConstant.SESSION_CAPTCHA_ID);
        if (!localCode.equals(code)) {
            return DeployConstant.USER_LOGIN_CAPTCHA_FAIL;
        }
        return doLogin(username, password);
    }


    @RequestMapping("/loginQuit")
    @ResponseBody
    public void loginQuit() {
        try {
            HttpSession session = getSession();
            session.removeAttribute(DeployConstant.USER_SESSION_ID);
            session.invalidate();
            getResponse().sendRedirect(getRequest().getContextPath() + "/login");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 登录方法
     *
     * @param name 账号
     * @param pass 密码
     * @return
     */
    private Integer doLogin(String name, String pass) {
        Map<String, UserModel> userMap = AuthLoad.userMap;
        if (CollectionUtil.isEmpty(userMap)) {
            return DeployConstant.USER_LOGIN_FAIL;
        }
        UserModel userModel = userMap.get(name);
        if (userModel == null) {
            return DeployConstant.USER_LOGIN_FAIL;
        }
        if (!pass.equals(userModel.getUserPass())) {
            return DeployConstant.USER_LOGIN_FAIL;
        }
        getSession().setAttribute(DeployConstant.USER_SESSION_ID, userModel);
        return DeployConstant.USER_LOGIN_SUCCESS;
    }


}
